<?php

namespace backend\controllers;

use Dompdf\Dompdf;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\DocumentProperties;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Alignment;
use TCPDF;


class DompdfController extends BaseController
{
    public function actionPdf1()
    {
        // 创建新的演示文稿
        $objPHPPowerPoint = new PhpPresentation();

        // 设置文档属性
        $objPHPPowerPoint->getDocumentProperties()
            ->setCreator("Your Name")
            ->setLastModifiedBy("Your Name")
            ->setTitle("Sample Presentation")
            ->setSubject("PHPPresentation Example")
            ->setDescription("Example of PHPPresentation to PDF conversion");

        // 创建幻灯片
        $currentSlide = $objPHPPowerPoint->getActiveSlide();

        // 添加内容到幻灯片
        $shape = $currentSlide->createRichTextShape()
            ->setHeight(300)
            ->setWidth(600)
            ->setOffsetX(10)
            ->setOffsetY(10);
        $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
        $textRun = $shape->createTextRun('Hello World!');
        $textRun->getFont()->setBold(true)
            ->setSize(40)
            ->setColor(new Color('FF0000'));

        // 保存为临时PPTX文件
        $tempFile = tempnam(sys_get_temp_dir(), 'phpppt');
        $oWriterPPTX = IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
        $oWriterPPTX->save($tempFile);

        // 使用TCPDF将PPTX转换为PDF
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetTitle('Converted Presentation');

        // 添加一页
        $pdf->AddPage();

        // 获取PPTX文件内容
        $content = file_get_contents($tempFile);

        // 这里需要实现PPTX到PDF的实际转换逻辑
        // 注意：TCPDF本身不支持直接转换PPTX，这里需要额外的处理
        // 以下是一个简单的替代方案，只是添加文本而不是真正的转换
        $pdf->Write(0, 'This is a placeholder for the converted presentation', '', 0, 'C', true);

        // 输出PDF
        $pdf->Output('presentation.pdf', 'D');

        // 删除临时文件
        unlink($tempFile);
    }

}
